//
//  SqliteHelper.m
//  ItboyeLibrary
//
//  Created by hebidu on 15/9/21.
//  Copyright © 2015年 Boye. All rights reserved.
//

#import "SqliteHelper.h"

@interface SqliteHelper()


@property NSString * lastDbPath;

@end

@implementation SqliteHelper


+(SqliteHelper *)sharedInstance{
    
    static SqliteHelper * sharedSqliteHelperInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedSqliteHelperInstance = [[self alloc] init];
    });
    
    return sharedSqliteHelperInstance;
}


-(BOOL) openDB:(NSString * )dbPath{
    if (self.database != nil) {
        if (self.lastDbPath != nil && [dbPath isEqualToString:self.lastDbPath]) {
            
            return YES;
        }
        
        if(![self closeDB]){
            return NO;
        }
    }
    self.database  = [FMDatabase databaseWithPath:dbPath];
    if (![self.database open]) {
        
        NSLog(@"Open database failed");
        return NO;
        
    }
    
    return YES;
}


-(BOOL) closeDB{
    return [self.database close];
}

-(NSString *) lastErrorMessage{
    return self.database.lastErrorMessage;
}


@end
